//////////////////////////////////////////////////////////////////////////////////
//
// ActionScript 3.0 Common Library (MIT License)
// Copyright (c) 2009 AS3CommonCode | Tony Birleffi | http://www.mosaiceye.com
// 
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////////

package org.as3commoncode.utils
{
	import flash.display.DisplayObject;	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.geom.Rectangle;
	import flash.geom.Point;	

	/**
	 * Utility class for using bitmap manipulation.
	 * 
	 * @author Tony Birleffi
	 */
	public class BitmapUtil 
	{
		/**
		 * Takes a snapshot of any sprite and then copy it over to any sprite you pass.
		 *	
		 * @example
		 * <listing>
		 * BitmapUtil.takeSnapShot(mc, copy, 500, 500, true, 0xffffff, null);
		 * </listing>
		 * 
		 *	@param	target			anything you wish to make a copy of.
		 *	@param	copy			the sprite you want to add it to.
		 *	@param	width			defined width area.
		 *	@param	height			defined height area.
		 *	@param	useAlpha		[optional] true has alpha, default is true.
		 *	@param	alphaColor		[optional] if you set the alpha to false, and define a color here that will appear, default is 0xffffff.
		 *	@param defineRect		[optional] define a custom rectangle to use rather than the default one.
		 */
		public static function takeSnapShot(target:Sprite, copy:Sprite, width:Number = NaN, height:Number = NaN, 
										     useAlpha:Boolean = true, alphaColor:Number = 0xffffff, defineRect:Rectangle = null):void
		{
			// Define values
			var newWidth:Number = (isNaN(width)) ? target.width : width;
			var newHeight:Number = (isNaN(height)) ? target.height : height;
			
			// Define the bmp A data.
			var bmdA:BitmapData = new BitmapData(newWidth, newHeight, useAlpha, alphaColor);
			bmdA.draw(target);
			
			// Define bmp B data.
			var bmdB:BitmapData = new BitmapData(newWidth, newHeight, useAlpha, alphaColor);
			
			// Define the area.
			var pt:Point = new Point(0, 0);
			var rect:Rectangle;
			if(defineRect) 
			{
				rect = defineRect;
				pt.x = rect.x;
				pt.y = rect.y;
			}
			
			target.localToGlobal(pt);
			rect = new Rectangle(pt.x, pt.y, newWidth, newHeight);
			
			// Copy the pixels.
			bmdB.copyPixels(bmdA, rect, pt);
			
			// Make the copy and add the bmp to the copy sprite.
			var bm1:Bitmap = new Bitmap(bmdA);
			copy.addChild(bm1);
		}
		
		/**
		 * Takes a snapshot of any sprite and return the bitmap.
		 *	
		 * @example
		 * <listing>
		 * BitmapUtil.getBitmapCopy(mc, 500, 500, true, 0xffffff, null);
		 * </listing>
		 * 
		 *	@param	target			anything you wish to make a copy of.
		 *	@param	width			defined width area.
		 *	@param	height			defined height area.
		 *	@param	useAlpha		[Optional], true has alpha, default is true.
		 *	@param	alphaColor		[Optional], if you set the alpha to false, and define a color here that will appear, default is 0xffffff.
		 *	@param  defineRect		[Optional], define a custom rectangle to use rather than the default one.
		 *	
		 *	@return Bitmap.
		 */
		public static function getBitmapCopy(target:DisplayObject, width:Number = NaN, height:Number = NaN, 
										     useAlpha:Boolean = true, alphaColor:Number = 0xffffff, defineRect:Rectangle = null):Bitmap
		{
			// Define values
			var newWidth:Number = (isNaN(width)) ? target.width : width;
			var newHeight:Number = (isNaN(height)) ? target.height : height;
			
			// Define the bmp A data.
			var bmdA:BitmapData = new BitmapData(newWidth, newHeight, useAlpha, alphaColor);
			bmdA.draw(target);
			
			// Define bmp B data.
			var bmdB:BitmapData = new BitmapData(newWidth, newHeight, useAlpha, alphaColor);
			
			// Define the area.
			var rect:Rectangle;
			if(defineRect) rect = defineRect;
			else
			{
				var pt:Point = new Point(0, 0);
				target.localToGlobal(pt);
				rect = new Rectangle(pt.x, pt.y, newWidth, newHeight);
			}
			
			// Copy the pixels.
			bmdB.copyPixels(bmdA, rect, pt);
			
			// Make the copy and add the bmp to the copy sprite.
			var bm1:Bitmap = new Bitmap(bmdA);
			
			// Return the bitmap.
			return bm1;
		}
		
		/**
		 * Method to return each library item as a bitmap.
		 * 
		 * @param target	which class.
		 * @return Bitmap.
		 */
		public static function asBitmap(target:Class):Bitmap 
		{
			var classRef:Class = target as Class;
			var retval:Bitmap = new classRef();
			return retval;
		}
	}
}